﻿using MediatR;
using T_Person.APi.Applation.CommandHandlers.PersonnelHandlers;
using T_Person.BaseRep.Interface;

namespace T_Person.APi.Applation.Commands.PersonnelCommand
{
    public class PersonnelUpdateByIdCommand : IRequestHandler<PersonnelUpdateByidHandlers, int>
    {
        private readonly IPersonnelRep personnelRep;
        private readonly IEmployeeRep employeeRep;

        public PersonnelUpdateByIdCommand(IPersonnelRep personnelRep, IEmployeeRep employeeRep)
        {
            this.personnelRep = personnelRep;
            this.employeeRep = employeeRep;
        }

        public async Task<int> Handle(PersonnelUpdateByidHandlers request, CancellationToken cancellationToken)
        {
            var lists = await personnelRep.GetAloneByid(request.id);
            lists.AuditState=request.state;
            if (request.state==2)
            {
                var employeelist = await employeeRep.GetAloneByresult(x => x.Ename == lists.EmployeeId && !x.IsDel);
                employeelist.EmployeeStyle++;
                employeelist.AuditState=request.state;
                await employeeRep.Update(employeelist);
            }
            lists.UpdateDate=DateTime.Now;
            lists.UpdateBy = "小红";
            return await personnelRep.Update(lists);
        }
    }
}
